import Vuex from 'vuex'


const store = new Vuex.Store({
  state: {
    loginInfo: {},
    bankInfo:{},
    role: [],
    detailData: [],
    orderId: 0,
    changedOrderId: 0,
    changedKnowledgeId: 0,
    user: '',
    loginUserNickname: '',
    loginUserAvatar: '',
    activeIndex: '1',
    publishActiveIndex: '1',
    userActiveIndex: '1-1',
    token: localStorage.getItem('token') !== null ? localStorage.getItem('token') : '',
    // localStorage.getItem('token') 表示从浏览器本地存储中获取名为 token 的值。
    // 浏览器本地存储可以通过 localStorage 对象来操作，其中的 getItem 方法用于获取指定键（即 token）对应的值。
    // 如果本地存储中不存在名为 token 的值，则 getItem 方法返回 null。
    paymentInfo: '',
    // imgShowRoad: process.env.VUE_APP_Address,//常量，.env.production文件中有具体定义
    // fileUploadRoad: process.env.VUE_APP_Address,
    mutiFile: '',
    username: '',
    avatar:''
  },
  getters: {
    getStorage(state) {
      if (state.token == '') {
        state.token = JSON.parse(localStorage.getItem('token'))   //这里的'token'原本是key 
        // 这里的JSON.parse是一个JavaScript的方法，它可以将一个JSON格式的字符串转换为一个JavaScript对象
        return state.token
      }
    },
    isExpert(state) {
      for (var k in state.role) {
        if (state.role[k].authority === "expert") {
          return true;
        }
      }
      return false;
    },
    isAdmin(state) {
      for (var k in state.role) {
        if (state.role[k].authority === "admin") {
          return true;
        }
      }
      return false;
    },
  },
  //修改状态store的唯一方法
  mutations: {
    updateOrderId(state, value) {
      state.orderId = value
    },
    updateusername(state, value) {
      state.username = value
    },
    updateDetailData(state, value) {
      state.detailData = value
    },
    updateChangedOrderId(state, value) {
      state.changedOrderId = value
    },
    updateChangedKnowledgeId(state, value) {
      state.changedKnowledgeId = value
    },
    updateLoginUserNickname(state, value) {
      state.loginUserNickname = value
    },
    updateLoginUserAvatar(state, value) {
      state.loginUserAvatar = value
    },
    updateRole(state, value) {
      state.role = value
    },
    updatePaymentInfo(state, value) {
      state.paymentInfo = value
    },
    updateActiveIndex(state, value) {
      state.activeIndex = value
    },
    updatePublishActiveIndex(state, value) {
      state.publishActiveIndex = value
    },
    updateUserActiveIndex(state, value) {
      state.userActiveIndex = value
    },
    // 设置存储token
    setToken(state, value) {
      state.token = value;
      //localStorage 对象是浏览器提供的本地存储机制.
      //setItem 方法是 localStorage 对象的一个方法，用于将一个键值对存储到本地存储中，其中第一个参数是键名，第二个参数是键值。
      localStorage.setItem('token', value)
    },
    // 删除token
    removeStorage(state) {
      state.token = ''
      localStorage.removeItem('token');
    },
    //重置state
    resetState(state) {
      state.role = []
      state.detailData = []
      state.orderId = 0
      state.changedOrderId = 0
      state.changedKnowledgeId = 0
      state.user = ''
      state.loginUserNickname = ''
      state.loginUserAvatar = ''
      state.activeIndex = '1'
      state.publishActiveIndex = '1'
    }
  },
  actions: {
  },
  modules: {
  }
})


export default store